/**
 * 基本页面类，所有页面继承此类
 */
var util = require('../utils/util.js');
var common = require('../utils/common.js');
var ajax = require('../utils/ajax.js');
// var Copyright = require('../components/copyright/index.js');
var app = getApp();

module.exports = {

  onLoad(options) {
    var {
			recommend
		} = options;

    recommend && app.setRecommend(recommend);
    // this._copyright = new Copyright({
    //   page: this
    // });
    this._ajaxSetStoreInfo();

  },

//获取总店信息
_ajaxSetStoreInfo: function() {
  var info = app.getStoreInfo();
  if (Object.keys(info).length == 0) {
  ajax.P({
    url: 'm=shop&c=index&a=info',
    data: {
      id: 0
    },
    success: (res) => {
      var data = res.data;
      app.setStoreInfo(data);
    }
    });
  }
  else {
   
  }
  // ajax.P({
  //   url: 'm=shop&c=index&a=info',
  //   data: {
  //     id: 0
  //   },
  //   success: (res) => {
  //     debugger;
  //     var data = res.data;
  //     app.setStoreInfo(data);
  //   }
  // });

},

	/**
	 * 登陆成功后处理
	 * @return {[type]} [description]
	 */
  $loginSuc() {
    // if (this._copyright) {
    //   this._copyright.init();
    // }
  },

  // 调用微信支付接口
  $wxPay: function (param) {
    wx.requestPayment({
      'timeStamp': param.timeStamp,
      'nonceStr': param.nonceStr,
      'package': param.package,
      'signType': 'MD5',
      'paySign': param.paySign,
      success: function (res) {
        param.success && param.success(res);
      },
      fail: function (res) {
        if (res.errMsg === 'requestPayment:fail cancel' || 'requestPayment:fail') {
          return;
        }
        if (param.fail) {
          param.fail(res);
        } else {
          util.alert(res.errMsg)
        }
      }
    })
  },

  //调用电话接口
  _callPhone: function (evt) {
    var {
			phone
		} = evt.currentTarget.dataset;
    var formId = evt.detail.formId;
    wx.makePhoneCall({
      phoneNumber: phone
    })
    common.submitForm(formId);
  },

  /**
 * 扫码下单
 * @return {[type]} [description]
 */
  _scanCode() {
    wx.scanCode({
      success: (res) => {
        console.log(res);
        wx.navigateTo({
          url: '/' + res.path
        });
      }
    });
  },

  //复制文本
  _copyText(evt) {
    var {
			copyVal,
      copySuccess
		} = evt.currentTarget.dataset;
    var formId = evt.detail.formId;

    wx.setClipboardData({
      data: copyVal,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            if (copySuccess) {
              util.showOK(copySuccess || '复制成功');
            }
          }
        })
      }
    })
    common.submitForm(formId);
  },

  //跳转到地图位置
  _toMap(evt) {
    var shop = evt.currentTarget.dataset;
    var formId = evt.detail.formId;

    this.toMap(shop, formId);
  },

  toMap(shop, formId) {
    wx.showLoading({
      title: '地图打开中...'
    });

    ajax.G('/openapi/CAddress/getLocation', {
      addr: shop.address
    }, function (data) {
      var addressInfo;

      wx.hideLoading();

      try {
        addressInfo = JSON.parse(data.result)['result'];
      } catch (e) { }

      if (!addressInfo) {
        return;
      }

      wx.openLocation({
        latitude: addressInfo.location.lat,
        longitude: addressInfo.location.lng,
        name: shop.name,
        address: shop.address,
      });
    });
    if (formId) {
      common.submitForm(formId);
    }
  },

  //提交表单
  _formSubmit(e) {
    var _self = this;
    var formId = e.detail.formId;
    var obj = e.detail.value;
    var params = {
      tableId: obj.tableId,
      values: [],
      contents: [],
      types: [],
      keynames: [],
    };

    Object.keys(obj).forEach(function (key) {
      var val = obj[key];

      if (/values\d+/.test(key)) {
        params.values.push(val);
      } else if (/contents\d+/.test(key)) {
        params.contents.push(val);
      } else if (/types\d+/.test(key)) {
        params.types.push(val);
      } else if (/keynames\d+/.test(key)) {
        params.keynames.push(val);
      }
    });

    ajax.P('/openapi/CUser/addTableContent', params, function (res) {

      util.alert('提交成功', function () {
        _self.setData({
          mFormFlush: !_self.data.mFormFlush
        });
        setTimeout(function () {
          _self.setData({
            mFormFlush: !_self.data.mFormFlush
          });
        }, 0);
      });

    });
    common.submitForm(formId);
  },
  // 跳转
  _jumpByLink(evt) {
    var {
			linkType,
      linkData
		} = evt.currentTarget.dataset;
    var formId = evt.detail.formId;

    if (linkType == 'goods') {
      wx.navigateTo({
        url: '/pages/goods/goods?goodsId=' + linkData.id
      })
    } else if (linkType == 'article') {
      wx.navigateTo({
        url: '/pages/article/article?artId=' + linkData.id
      })
    } else if (linkType == 'page') {
      common.goToPage(linkData.id);
    } else if (linkType == 'act') {
      util.alert('该功能暂未开放');
    } else if (linkType == 'goodsCate') {
      wx.navigateTo({
        url: '/pages/goodsList/goodsList?ty=' + linkData.category_id
      })
    } else if (linkType == 'phone') {
      wx.makePhoneCall({
        phoneNumber: linkData.phone
      });
    }
    common.submitForm(formId);
  },

  _jumpByUrl: function(evt){
    var {
      url,
      linkType
    } 
     = evt.currentTarget.dataset;
    if(linkType = "tab"){
      wx.switchTab({
        url: url
      })
    }
    else{
      wx.navigateTo({
        url: url
      })
    }

  },

  _jump: function (evt) {
    common.jump(evt);
  },

  _showMsg(e) {
    var {
			msg
		} = e.target.dataset;

    util.alert(msg);
  },

  //预览图片
  _previewImg: function (evt) {
    var {
			current,
      urls
		} = evt.currentTarget.dataset;

    wx.previewImage({
      current,
      urls,
    })
  },
};